package wsi.task.typedetector;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.rygh.core.html.table.TableTuple;

import wsi.dataguide.tree.WSXmlLeaf;

public class TypeInfo {

	private final WSXmlLeaf node;
	private final Map<String, Double> typeMap = new HashMap<>();
	private Set<String> setValue;

	public TypeInfo(WSXmlLeaf node) {
		this.node = node;
	}

	public void addType(String type, double conf) {
		typeMap.put(type, conf);
	}

	public TableTuple getCells() {
		TableTuple tuple = new TableTuple();

		tuple.add(node.getValue().getName());

		if (typeMap.isEmpty()) {
			tuple.add("/");
			tuple.add("/");
		} else
			for (Entry<String, Double> e : typeMap.entrySet()) {
				if (e.getKey().equals("set"))
					tuple.add(e.getKey() + " " + setValue);
				else
					tuple.add(e.getKey());

				tuple.add(String.format("%.2f", e.getValue()));
			}

		tuple.add(node.getXpath().getValue());

		return tuple;
	}

	public WSXmlLeaf getNode() {
		return node;
	}

	protected Set<String> getSetValue() {
		return setValue;
	}

	public Map<String, Double> getTypeMap() {
		return typeMap;
	}

	protected void setSetValue(Set<String> set) {
		setValue = new HashSet<>();
		setValue.addAll(set);
	}

	@Override
	public String toString() {
		StringBuilder sbuilder = new StringBuilder();

		sbuilder.append(node.getValue().getName());
		sbuilder.append("\r\n");
		sbuilder.append(node.getXpath());
		sbuilder.append("\r\n");

		if (typeMap.isEmpty())
			sbuilder.append("\tnone.\r\n");
		else
			for (Entry<String, Double> e : typeMap.entrySet()) {
				sbuilder.append(String.format("\t%s: %.2f  ", e.getKey(),
						e.getValue()));

				if (e.getKey().equals("set"))
					sbuilder.append(setValue);

				sbuilder.append("\r\n");
			}

		return sbuilder.toString();
	}

}
